// OGoMojo
// Whitemice.MOGI.Server
// License: LGPL
// Copyright: Whitemice Consulting, 2006
// Author(s): Adam Tauno Williams (awilliam@whitemice.org)
using System;
using System.Net;
using System.Web;
using System.Collections;
using log4net;

namespace Whitemice.MOGI 
{
	public abstract class AsyncServer : Whitemice.MOGI.Server, Whitemice.MOGI.IAsyncServer
	{
		public AsyncServer(string logName) : base(logName)
		{
		}
	
		public void AsyncSearch(string criteria, AsyncSearchCallback callback, object context)
		{
			switch ((criteria.Split('/'))[0])
			{
				case "Document":
					if (!SupportsOperation(MOGI.DetermineType(criteria), Operation.Fetch))
						throw new NotSupportedException("Search is not supported for this document type.");
					if (criteria.Split('/')[2] == "Favorites")
						if (!SupportsOperation(MOGI.DetermineType(criteria), Operation.GetFavorites))
							throw new NotSupportedException("Favorite retrieval is not supported for this document type.");							
					ServerAsyncSearch(MOGI.DetermineType(criteria), ((criteria.Split('/'))[2]).Split('~'), callback, context);
					break;
				default:
					throw new NotSupportedException("Async search only supported on top level documents");
			}
		}
		
		public void AsyncGet(string documentId, AsyncSearchCallback callback, object context)
		{
			if (!SupportsOperation(MOGI.DetermineType(documentId), Operation.Fetch))
				throw new NotSupportedException("Search is not supported for this document type.");
			ServerAsyncGet(documentId, callback, context);
		}
		
		protected abstract void ServerAsyncSearch(DocumentType documentType, 
		 																		string[] criteria,
		                                    AsyncSearchCallback callback, 
		                                    object context);
		                                    
		protected abstract void ServerAsyncGet(string documentName,
		                                    AsyncSearchCallback callback, 
		                                    object context);
	}
}